package com.tiantian.subject.controller;

import cn.hutool.core.collection.CollUtil;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.subject.domain.dto.AddFolderDto;
import com.tiantian.subject.domain.dto.DeleteFileDto;
import com.tiantian.subject.domain.vo.QuestionBankFileVo;
import com.tiantian.subject.service.QuestionBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tiantian
 */
@RequiredArgsConstructor
@RequestMapping("/topic/questionBank")
@RestController
@Validated
public class QuestionBankController {

    private final QuestionBankService questionBankService;

    @GetMapping("/list")
    public ResponseResult<QuestionBankFileVo> getFileList(String curPath) {
        QuestionBankFileVo fileList = questionBankService.getFileList(curPath);
        return ResponseResult.ok(fileList);
    }

    @PostMapping("/addFolder")
    public ResponseResult<Void> addFolder(@Validated AddFolderDto addFolderDto) {
        questionBankService.AddFolder(addFolderDto);
        return ResponseResult.ok();
    }

    @PostMapping("/delFiles")
    public ResponseResult<Boolean> deleteFile(@RequestBody DeleteFileDto deleteFileDto) {
        List<String> fileNames = deleteFileDto.getFileNames();
        if (CollUtil.isEmpty(fileNames)) {
            return ResponseResult.fail("删除的文件数组不能为空");
        }
        String[] fileNamesArr = fileNames.toArray(new String[0]);
        boolean isDel = questionBankService.deleteDirOrFile(fileNamesArr);
        return ResponseResult.ok(isDel);
    }

}
